<template>
  <a-popconfirm v-if="isCheck" title="确定删除吗?" @confirm="() => batchDel()">
    <a-button  type="danger" :disabled="renderOptions.disabled" ghost icon="minus"  class="theme--perfect-button">{{defaultProps.title}}</a-button>
  </a-popconfirm>
</template>

<script>
export default {
  name: 'VxeToolbarDel',
  props: {
    renderOptions: Object,
    params: Object,
    toolbarProps: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      defaultProps: {
        title: '删除'
      },
      isCheck: false,
      unwatch: ''
    }
  },
  methods: {
    batchDel() {
      const { events = {}} = this.renderOptions
      const { button, $grid } = this.params
      events.click(button, $grid)
    }
  },
  mounted() {
    this.defaultProps = Object.assign({}, this.defaultProps, this.toolbarProps)
    this.unwatch = this.$watch(
      function() {
        // 处理函数都会被调用。
        // 这就像监听一个未被定义的计算属性
        const { $grid } = this.params
        const checkbox = $grid.getCheckboxRecords(true) || []
        return checkbox
      },
      function(newVal, oldVal) {
        // 将监听放在这里，计算属性中监听有bug
        this.isCheck = newVal.length > 0
      }
    )
  },
  beforeDestroy() {
    this.unwatch()
  }

}
</script>
<style>
.z-vxe-toolbar .vxe-button--item{
  margin-left: 0px !important;
}
</style>

